package com.stone.framework.eai.handler;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.http.inbound.RequestMapping;
import org.springframework.integration.webflux.inbound.WebFluxInboundEndpoint;
import org.springframework.integration.webflux.outbound.WebFluxRequestExecutingMessageHandler;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.stone.framework.message.ResMessage;

import reactor.core.publisher.Mono;

@Configuration
public class TestHandler {

    public Mono<ServerResponse> test(ServerRequest request) {
        return ServerResponse.ok().bodyValue(ResMessage.success());
    }

    @Bean
    public WebFluxInboundEndpoint testInboundEndpoint() {
        WebFluxInboundEndpoint endpoint = new WebFluxInboundEndpoint();
        RequestMapping requestMapping = new RequestMapping();
        requestMapping.setMethods(HttpMethod.POST);
        requestMapping.setPathPatterns("/test");
        endpoint.setRequestMapping(requestMapping);
        endpoint.setRequestChannel(testChannel());
        return endpoint;
    }

    @Bean
    public DirectChannel testChannel() {
        DirectChannel channel = new DirectChannel();
        return channel;
    }

    @Bean
    public DirectChannel directChannel() {
        return new DirectChannel();
    }

    // @Transformer(inputChannel = "testChannel", outputChannel = "directChannel")
    // public String transformer(String str) {
    // return "test";
    // }

    // @ServiceActivator(inputChannel = "testChannel")
    // public WebFluxRequestExecutingMessageHandler testOutboundEndpoint() {
    // WebFluxRequestExecutingMessageHandler handler = new WebFluxRequestExecutingMessageHandler("");
    // handler.setHttpMethod(HttpMethod.POST);
    // return handler;
    // }

    @ServiceActivator(inputChannel = "testChannel")
    public String testOutboundEndpoint(String payload) {
        return payload + "hello world";
    }

}
